/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package captchagenerator;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;

/**
 * 一次性生成静态验证码（4数字）图片。
 * 
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.0, Sep 18, 2012
 */
public class Test {
    
    public static void main(String[] args) throws IOException {
        final int width = 130, height = 200;
        final BufferedImage bufferedImage =
                new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        final Graphics graphics = bufferedImage.getGraphics();
        for (int i = 0; i < 10; i++) {
            graphics.setColor(getRandColor(200, 250));
            graphics.fillRect(0, i * 20, width, (i + 1) * 20);
        }
        
        graphics.setFont(new Font("Times New Roman", Font.PLAIN, 18));
        final Random random = new Random();
        
        graphics.setColor(getRandColor(160, 200));
        for (int i = 0; i < 15000; i++) {
            final int x = random.nextInt(width);
            final int y = random.nextInt(height);
            graphics.drawLine(x, y, x, y);
        }
        
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                graphics.setColor(getRandColor(20, 130));
                graphics.drawString(String.valueOf(j), j * 13 + 3, i * 20 + 16);
            }
        }
        
        graphics.setColor(getRandColor(200, 250));
        for (int i = 0; i < 2000; i++) {
            final int x = random.nextInt(width - 12);
            final int y = random.nextInt(height);
            graphics.drawLine(x, y, x, y);
        }
        
        graphics.dispose();
        final OutputStream outputStream = new FileOutputStream("captcha.png");
        
        ImageIO.write(bufferedImage, "png", outputStream);
        outputStream.close();
        
        for (int k = 0; k < 100; k++) {
            BufferedImage[] subs = new BufferedImage[4];
            StringBuilder name = new StringBuilder();
            
            for (int i = 0; i < 4; i++) {
                int x = getRand(13);
                int y = getRand(20);
                Rectangle re = new Rectangle(x, y, 13, 20);
                subs[i] = getSubImage(bufferedImage, re);
                
                name.append(x / 13);
            }
            
            for (int y = 0; y < 3; y++) {
                subs[y+1] = splice(subs[y], subs[y+1]);
            }
            
             ImageIO.write(subs[3], "jpg", new File("out/" + name.toString() + ".jpg"));
        }
    }

    /**
     * Splices the specified image1 and image2 horizontally.
     * 
     * @param image1 the specified image1
     * @param image2 the specified image2
     * @return the spliced image
     */
    private static BufferedImage splice(final java.awt.Image image1, final java.awt.Image image2) {
        final int[][] size = {{image1.getWidth(null), image1.getHeight(null)},
            {image2.getWidth(null), image2.getHeight(null)}};
        
        final int width = size[0][0] + size[1][0];
        final int height = Math.max(size[0][1], size[1][1]);
        
        final BufferedImage ret = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        final Graphics2D g2 = ret.createGraphics();
        g2.drawImage(image1, 0, 0, null);
        g2.drawImage(image2, size[0][0], 0, null);
        
        return ret;
    }
    
    private static int getRand(int n) {
        return new Random().nextInt(10) * n;
    }
    
    private static BufferedImage getSubImage(BufferedImage image, Rectangle subImageBounds) throws IOException {
        BufferedImage subImage = image.getSubimage(subImageBounds.x, subImageBounds.y,
                subImageBounds.width, subImageBounds.height);
        
        return subImage;
    }
    
    private static Color getRandColor(final int fc, final int bc) {
        final Random random = new Random();
        final int r = fc + random.nextInt(bc - fc);
        final int g = fc + random.nextInt(bc - fc);
        final int b = fc + random.nextInt(bc - fc);
        
        return new Color(r, g, b);
    }
}
